# @Remark:登录页面
# login_page.py模块下就是存放登录页面上的各个功能点的。（登录功能、获取登录失败的提示信息）

import time
from selenium.webdriver.common.action_chains import ActionChains

from Common.basepage import BasePage
from PageLocators.AR.ar_home_loc import ArAddLessonLocator as ar_add_loc
from PageLocators.AR.ar_home_loc import QuestionBankManagementLocator as qbm_loc
from PageLocators.AR.ar_home_loc import ExaminationPaperLocator as ep_loc
from PageLocators.AR.ar_home_loc import LessonLocator as les_loc
from PageLocators.AR.ar_home_loc import ArAddCourseLocator as cs_loc
from PageLocators.AR.ar_home_loc import CourseManagementLocator as cm_loc
from TestDatas.ArDatas.create_course_data import *
from TestDatas.ArDatas.ar_question_bank_management_datas import *
from PageObjects.tools import replace_loc
from Common.dir_config import audio_dir, img_dir



class ArHomePage(BasePage):
    # crm首页-学生服务
    def go_content(self):
        self.click_element(ar_loc.content_loc, "内容管理导航", timeout=90)

    # 跳转课时管理
    def go_lesson(self):
        self.click_element(ar_loc.lesson_loc, "课时管理导航")
        time.sleep(1)

    # 添加课时
    def add_lesson(self):
        self.click_element(ar_loc.add_lesson_loc, "添加课时")
        time.sleep(2)

    # 添加课时页面
    def input_or_select_lesson(self, lesson_name="UI课时名称", type_option="主课1V3"):
        ts = str(int(time.time()))
        self.input_text(ar_add_loc.lessonName_loc, "课时名称输入框", "{}{}".format(lesson_name, ts))
        self.input_text(ar_add_loc.class_times_loc, "课次输入框", 12)
        self.input_text(ar_add_loc.whenLong_loc, "时长输入框", 60)

        self.drop_down_not_select(ar_add_loc.subject_loc, ar_add_loc.mathematics_loc, "选择科目下拉框")
        self.drop_down_not_select(ar_add_loc.term_loc, ar_add_loc.term_chunshang_loc, "选择学季下拉框")
        self.drop_down_not_select(ar_add_loc.level_loc, ar_add_loc.level_p4_loc, "选适用水平下拉框")
        # self.drop_down_not_select(ar_add_loc.courseType_loc, ar_add_loc.courseType_shipin_loc, "选择课时类型下拉框")

        # 类型根据参数选择
        # self.drop_down_not_select(ar_add_loc.type_loc, ar_add_loc.type_1v3_loc, "选择类型下拉框")
        ar_add_loc.type_option_loc[1] = ar_add_loc.type_option_loc[1].format(type_option)
        type_option_loc = tuple(ar_add_loc.type_option_loc)
        self.drop_down_not_select(ar_add_loc.type_loc, type_option_loc, "选择类型下拉框-{}".format(type_option))

        self.drop_down_not_select(ar_add_loc.materialType_loc, ar_add_loc.materialType_zhu_loc, "选择教材类型下拉框")
        time.sleep(2)
        # js = "document.querySelectorAll('.md.header-md.header-collapse-none.hydrated')[2].nextElementSibling.scrollToBottom()"
        js = "document.querySelectorAll('.md.header-md.header-collapse-none.hydrated')[2].nextElementSibling.scrollToPoint(0, 500)"
        self.driver.execute_script(js)
        self.input_text(ar_add_loc.remark_loc, "备注信息", "UI写入备注")
        time.sleep(1)
        js_kind = 'document.getElementsByClassName("ql-editor ql-blank")[0].firstChild.innerHTML="UI写入简介" '
        self.driver.execute_script(js_kind)
        js = "document.querySelectorAll('.md.header-md.header-collapse-none.hydrated')[2].nextElementSibling.scrollToBottom()"
        self.driver.execute_script(js)
        time.sleep(1)

    # 写入简介
    def vocabulary_send_keys(self):
        actions = ActionChains(self.driver)
        self.click_element(ar_add_loc.vocabulary_loc)
        actions.send_keys("今日工作内容").perform()

    # 确认提交
    def confirm_submit(self):
        self.click_element(ar_add_loc.confirm_loc, "确定")
        time.sleep(0.5)
        toast = self.toast_wait_eleVisible(ar_add_loc.toast_loc, "toast提示11", timeout=2)
        if toast:
            print(toast)
            if "课时创建成功" in toast:
                return True
            else:
                return False
        return False


class LessonPage(BasePage):
    """课时管理页面"""
    def search_lesson_name(self, name="UI课时名称"):
        """搜索课时"""
        self.input_text(les_loc.lesson_name_loc, "课时管理页面课程名称", name)
        self.click_element(les_loc.search_btn_loc, "课时管理页-搜索")
        time.sleep(3)

    def checkbox_lesson(self, name="UI课时名称"):
        """勾选课时"""
        time.sleep(1)
        les_loc.checkbox_loc[1] = les_loc.checkbox_loc[1].format(name)
        checkbox_loc = tuple(les_loc.checkbox_loc)
        self.click_element(checkbox_loc, "勾选")

    def release_lesson(self):
        """发布课时"""
        time.sleep(3)
        self.click_element(les_loc.release_loc, "发布")


class CreateCoursePage(BasePage):
    """组课"""
    def go_course_page(self):
        self.click_element(les_loc.create_course_loc, "组课")
        time.sleep(2)

    def course_details(self, course_name="UI创建课程", type_option="主课1V3"):
        ts = str(int(time.time()))
        self.drop_down_not_select(cs_loc.subject_loc, cs_loc.mathematics_loc, "课程科目下拉框")
        # 类型根据参数选择
        # self.drop_down_not_select(cs_loc.type_loc, cs_loc.type_1v3_loc, "课程类型")
        cs_loc.type_option_loc[1] = cs_loc.type_option_loc[1].format(type_option)
        type_option = tuple(cs_loc.type_option_loc)
        self.drop_down_not_select(cs_loc.type_loc, type_option, "课程类型--{}".format(type_option))
        self.drop_down_not_select(cs_loc.lesson_package_loc, cs_loc.lesson_package_1_loc, "课时包")
        self.drop_down_not_select(cs_loc.garde_loc, cs_loc.garde_1_loc, "等级")
        self.drop_down_not_select(cs_loc.school_age_loc, cs_loc.school_small_low_loc, "课程学龄")
        contents_lst = [(cs_loc.contents_loc_list[0], cs_loc.contents_loc_list[1].format(content=con_loc)) for con_loc
                        in contents]
        print(contents_lst)
        self.drop_down_not_selects(cs_loc.courses_content_loc, "教学内容", *contents_lst, submit_loc=cs_loc.confirm_loc)

        self.drop_down_not_select(cs_loc.year_loc, cs_loc.select_year_loc, "课程-年份")

        self.drop_down_not_select(cs_loc.term_loc, cs_loc.term_chunshang_loc, "课程适用学季")
        self.input_text(cs_loc.course_name_loc, "课程名称", "{}{}".format(course_name, ts))
        try:
            self.input_text(cs_loc.course_describe_loc, "课程描述", "UI创建课程描述")
        except Exception as e:
            print(e)
        # self.drop_down_not_select(cs_loc.level_loc, cs_loc.level_p4_loc, "课程适用水平")

        self.click_element(cs_loc.channel_loc, "课程渠道")
        scroll = 0
        for i in range(10):
            if self.get_element(cs_loc.test_channel_loc):
                break
            scroll += 100
            js = "var q=document.getElementsByClassName('popover-viewport sc-ion-popover-md sc-ion-select-popover-h sc-ion-select-popover-s md hydrated')[0].scrollTop = {}".format(scroll)
            self.driver.execute_script(js)
        time.sleep(2)
        self.click_element(cs_loc.test_channel_loc, "选择测试渠道")

    # 写入课程备注
    def vocabulary_send_keys(self, remark="今日工作内容"):
        actions = ActionChains(self.driver)
        self.click_element(cs_loc.remark_loc, "课程备注")
        actions.send_keys(remark).perform()
        js = "document.querySelectorAll('.md.header-md.header-collapse-none.hydrated')[2].nextElementSibling.scrollToBottom()"
        self.driver.execute_script(js)

    def class_related(self):
        """上课相关"""
        self.drop_down_not_select(cs_loc.class_mode_loc, cs_loc.mode_Blingabc_loc, "上课方式")

    # 确认提交
    def confirm_submit(self):
        self.click_element(cs_loc.submit_confirm_loc, "确定")
        time.sleep(0.5)
        toast = self.toast_wait_eleVisible(ar_add_loc.toast_loc, "toast提示11", timeout=2)
        if toast:
            print(toast)
            if "添加成功" in toast:
                return True
            else:
                return False
        return False

    def search_course_name(self, name="UI课时名称"):
        """搜索课时"""
        self.input_text(ar_add_loc.course_name_loc, "课程管理页面课程名称", name)
        self.click_element(ar_add_loc.search_btn_loc, "课程管理页-搜索")
        time.sleep(3)

    def checkbox_course(self, name="付费会员"):
        """勾选课时"""
        time.sleep(1)
        ar_add_loc.checkbox_loc[1] = ar_add_loc.checkbox_loc[1].format(name)
        checkbox_loc = tuple(ar_add_loc.checkbox_loc)
        self.click_element(checkbox_loc, "勾选")

    def release_course(self, course_name="付费会员"):
        """发布课程"""
        time.sleep(1)
        self.click_element(ar_add_loc.release_loc, "发布")
        time.sleep(1)


class CourseManagementPage(BasePage):
    """AR课程管理页面"""
    def search_course_name(self, name=""):
        """课程课时"""
        self.input_text(cm_loc.course_name_loc, "课程管理页面课程名称", name)
        self.click_element(cm_loc.search_btn_loc, "课程管理页-搜索")

    def checkbox_course(self, name="UI课程名称"):
        """勾选课程"""
        cm_loc.checkbox_loc[1] = cm_loc.checkbox_loc[1].format(name)
        checkbox_loc = tuple(cm_loc.checkbox_loc)
        self.click_element(checkbox_loc, "勾选")

    def release_course(self):
        """发布课程"""
        self.click_element(cm_loc.release_loc, "发布")
        time.sleep(0.5)
        toast = self.toast_wait_eleVisible(cm_loc.toast_loc, "发布课程toast提示", timeout=2)
        if toast:
            print(toast)
            if "发布成功" in toast:
                return True
            else:
                return False
        return False


class QuestionBankManagementPage(BasePage):
    """AR题库管理模块"""
    def change_module_page(self, nb1_loc=ar_loc.question_bank_loc, nb2_loc=ar_loc.subject_loc,
                           attr_name='class', attr_value='parent-active', nb1_name='题库管理', nb2_name='题目管理'):
        """选择模块页面
        :param nb1_loc: 一级导航栏模块定位 # navigation bar 导航栏
        :param nb2_loc: 二级导航栏模块定位
        :param attr_name: 属性名
        :param attr_value: 一级导航栏展开的属性值，默认参数class属性值
        :param nb1_name: 一级导航模块名称
        :param nb2_name: 二级导航模块名称
        """
        time.sleep(5)
        attr_values = self.get_element_attribute(nb1_loc, attr_name, "获取导航栏属性值用于判断一级导航是否展开")
        print('attr_values----', attr_values)
        if attr_value in attr_values:
            self.click_element(nb2_loc, "点击AR二级导航-{}".format(nb2_name))
        else:
            self.click_element(nb1_loc, "点击AR一级导航-{}".format(nb1_name))
            time.sleep(1)
            self.click_element(nb2_loc, "点击AR二级导航-{}".format(nb2_name))
        time.sleep(2)

    def select_options_lst(self, select_loc, select_name):
        """
        获取下拉框选项
        :param select_loc: 下拉框定位
        :param select_name: 下拉框名称
        :return: 下拉框下所有选项值
        """
        self.click_element(select_loc, "新建题目点击【{}】下拉框".format(select_name))
        time.sleep(2)
        option_lst = self.get_element_text(qbm_loc.select_options_loc, "获取下【{}】拉框下的选项值".format(select_name))
        return option_lst

    def click_option(self, select_name, option_name):
        """
        点击下拉框选项值
        :param select_name: 下拉框名称
        :param option_name: 选项值名称
        :return:
        """
        time.sleep(1)
        option_loc = replace_loc(qbm_loc.option_loc, option_name)
        self.click_element(option_loc, "点击【{}】拉框下【{}】".format(select_name, option_name))
        time.sleep(3)

    def select_subject(self, question_type):
        """
        新建题目下拉选项操作
        :param question_type: 题型
        :return:
        """
        question_options = self.select_options_lst(qbm_loc.question_types_loc, '题型选择')
        print(question_options)
        if question_type == 'scratch':
            self.click_option("题型选择", qbm_option_data["scratch_check"])
        if question_type == 'python':
            self.click_option("题型选择", qbm_option_data["python_check"])
        level_options = self.select_options_lst(qbm_loc.levels_loc, '等级选择')
        print(level_options)
        self.click_option("等级选择", qbm_option_data["level"])
        diff_options = self.select_options_lst(qbm_loc.difficulty_loc, '难度选择')
        print(diff_options)
        self.click_option("难度选择", qbm_option_data["diff"])
        type_options = self.select_options_lst(qbm_loc.course_types_loc, '课程类型')
        print(type_options)
        self.click_option("课程类型", qbm_option_data["type"])
        time.sleep(1)

    def subject_input(self, question_type):
        """新建题目输入项
        :param question_type: 题型
        """
        if question_type == 'scratch':
            self.input_text(qbm_loc.subject_name_loc, "新增题目-输入题目名称", qbm_subject_scratch_name)
        if question_type == 'python':
            self.input_text(qbm_loc.subject_name_loc, "新增题目-输入题目名称", qbm_subject_python__name)
            self.input_text(qbm_loc.preset_code_loc, "新增题目-输入题目预置代码", qbm_preset_code_text)
            self.input_text(qbm_loc.reference_version_loc, "新增题目-输入参考答案", qbm_scale_rule_text)

        self.input_text(qbm_loc.create_name_loc, "新增题目-输入创建人", qbm_subject_create_name)
        self.input_text(qbm_loc.subject_text1, "新增题目-题目文本一", qbm_subject_text1)
        self.input_text(qbm_loc.subject_text2, "新增题目-题目文本二", qbm_subject_text2)
        self.input_text(qbm_loc.subject_text3, "新增题目-题目文本三", qbm_subject_text3)
        self.input_text(qbm_loc.knowledge_point_loc, "新增题目-知识点", knowledge_point_text)
        self.input_text(qbm_loc.scale_rule_loc, "新增题目-评分标准", scale_rule_text)
        time.sleep(3)

    def subject_upload(self, upload_loc, file_name, img_doc=''):
        """上传文件"""
        time.sleep(3)
        img_path = img_dir + file_name
        print("路径地址---", img_path)
        time.sleep(2)
        self.upload_file(upload_loc, img_path, img_doc)
        time.sleep(30)

    def save_toast(self, save_loc=qbm_loc.save_loc, save_img_doc="点击保存并继续按钮", toast_loc=qbm_loc.save_toast_loc):
        """保存并验证toast"""
        try:
            self.click_element(save_loc, save_img_doc)
            time.sleep(1)
            # toast = self.get_element_text(toast_loc, "获取toast弹窗文本", timeout=2)
            toast = self.base_find(toast_loc, "获取toast弹窗文本", timeout=2).get_attribute('innerText')
        except Exception as e:
            print(e)
            toast = ""
        print('toast====', toast)
        return toast

    def subject_create_scratch(self, nb2_loc, nb2_name, question_type='scratch'):
        """
        题目管理-新增题目-scratch操作题
        :param nb2_loc: 二级导航栏模块定位
        :param nb2_name: 二级导航模块名称
        :param question_type: 题型
        :return:
        """
        self.change_module_page(nb2_loc=nb2_loc, nb2_name=nb2_name)  # 选择模块页面
        self.click_element(qbm_loc.subject_add_loc, "点击题目管理-添加")  # 点击添加-到新增题目页面
        time.sleep(3)
        self.select_subject(question_type)  # 新建题目下拉选项操作
        time.sleep(3)
        self.subject_input(question_type)  # 新建题目输入框项
        time.sleep(3)
        self.subject_upload(qbm_loc.upload_subject_cimg_loc, "1.jpg", "上传题目图片")  # 上传图片
        time.sleep(3)
        if question_type == 'scratch':
            self.subject_upload(qbm_loc.upload_answers_cimg_loc, "2.jpg", "上传参考答案")
            time.sleep(3)
        toast = self.save_toast()
        time.sleep(60)

    def subject_lst(self, nb2_loc, nb2_name):
        """题目管理列表"""
        self.change_module_page(nb2_loc=nb2_loc, nb2_name=nb2_name)  # 选择模块页面
        time.sleep(5)
        self.input_text(qbm_loc.subject_search_name_loc, "题目名称搜索框", qbm_search_subject_name)
        self.input_text(qbm_loc.subject_search_id_loc, "题目ID搜索框", qbm_search_subject_ID)
        self.select_options_lst(qbm_loc.search_question_types_loc, "列表搜索-题型下拉框")
        self.click_option("列表搜索-题型下拉框", qbm_search_question_type)
        self.select_options_lst(qbm_loc.search_course_types_loc, "列表搜索-课程类型下拉框")
        self.click_option("列表搜索-课程类型下拉框", qbm_search_course_type)
        self.select_options_lst(qbm_loc.search_levels_loc, "列表搜索-等级下拉框")
        self.click_option("列表搜索-等级下拉框", qbm_search_level)
        self.select_options_lst(qbm_loc.search_difficulty_loc, "列表搜索-难度下拉框")
        self.click_option("列表搜索-难度下拉框", qbm_search_difficulty)
        self.input_text(qbm_loc.subject_created_loc, "创建人搜索框", qbm_search_created)
        self.click_element(qbm_loc.search_button_loc, "题目管理-列表搜索-点击搜索")
        time.sleep(5)
        lst_table_text = self.get_element_text(qbm_loc.lst_body_loc, "列表数据")
        if "UI自动化" not in lst_table_text:
            print("未搜索到数据")
            raise
        self.click_element(qbm_loc.reset_button_loc, "题目管理-列表搜索-点击重置按钮")
        time.sleep(5)
        self.input_text(qbm_loc.subject_created_loc, "创建人搜索框", qbm_search_created)
        self.click_element(qbm_loc.search_button_loc, "题目管理-列表搜索-点击搜索")
        time.sleep(5)
        # el = self.get_element(qbm_loc.lst_body_row_loc, "获取列表控件元素")
        el = self.driver.find_elements(*qbm_loc.lst_body_row_loc)
        print("列表数据搜索条数【", len(el), "条】")
        self.click_element(qbm_loc.del_row_loc, "点击列表数据删除按钮")
        time.sleep(5)
        self.click_element(qbm_loc.alert_del_loc, "点击确认删除")

    def select_examination_paper(self):
        """
        新建试卷下拉选项操作
        :param question_type: 题型
        :return:
        """
        self.select_options_lst(ep_loc.ep_select_types_loc, "新增试卷-点击适用课程类型")
        self.click_option("新增试卷-选择适用课程类型", ep_course_type)
        self.select_options_lst(ep_loc.ep_select_levels_loc, "新增试卷-点击适用等级")
        self.click_option("新增试卷-选择适用等级", ep_level)
        self.select_options_lst(ep_loc.ep_select_difficulty_loc, "新增试卷-点击试卷难度级别")
        self.click_option("新增试卷-选择试卷难度级别", ep_diff)

    def examination_paper_input(self):
        """新建试卷输入项
        :param question_type: 题型
        """
        self.input_text(ep_loc.ep_name_loc, "输入新增试卷-试卷名称", ep_name_data)
        self.input_text(ep_loc.ep_created_loc, "输入新增试卷-创建人", ep_created_data)
        self.input_text(ep_loc.ep_result_chinese_copy_loc, "输入新增试卷-创建人", ep_created_data)
        time.sleep(3)

    def add_subject(self):
        """试卷管理-新增试卷-添加题目"""
        self.click_element(ep_loc.ep_add_subject_loc, "新增试卷-点击添加题目")
        time.sleep(3)
        self.input_text(ep_loc.ep_choice_window_subject_name_loc, "新增试卷-添加题目-题目名称输入框", ep_window_subject_name_data)
        time.sleep(3)
        self.click_element(ep_loc.ep_choice_window_search_loc, "新增试卷-添加题目-搜索")
        time.sleep(5)
        self.click_element(ep_loc.ep_choice_window_checkbox_loc, "新增试卷-添加题目-全选")
        time.sleep(2)
        self.click_element(ep_loc.ep_choice_window_save_loc, "新增试卷-添加题目-保存")
        time.sleep(5)

    def create_examination_paper(self):
        """
        试卷管理-新增试卷
        :return:
        """
        self.click_element(ep_loc.add_ep_loc, "点击试卷管理-新增试卷")
        time.sleep(5)
        self.examination_paper_input()  # 新建试卷输入项
        self.select_examination_paper()  # 新建试卷下拉选项操作
        self.add_subject()  # 试卷管理-新增试卷-添加题目
        toast = self.save_toast(save_loc=ep_loc.ep_save_loc, save_img_doc="试卷管理-新增试卷-保存", toast_loc=ep_loc.ep_toast_loc)

    def examination_paper_lst(self):
        """试卷管理列表页"""
        """题目管理列表"""
        self.change_module_page(nb2_loc=qbm_examination_paper_module_params["nb2_loc"],
                                nb2_name=qbm_examination_paper_module_params["nb2_name"])  # 选择模块页面
        time.sleep(5)
        self.input_text(ep_loc.ep_lst_name_input_loc, "试卷名称搜索框", ep_lst_name_data)
        self.input_text(ep_loc.ep_lst_id_input_loc, "试卷ID搜索框", ep_lst_id_data)
        self.input_text(ep_loc.ep_lst_created_input_loc, "试卷创建人搜索框", ep_created_data)
        self.click_element(ep_loc.ep_lst_search_loc, "试卷管理-列表搜索-点击搜索")
        time.sleep(5)
        lst_table_text = self.get_element_text(qbm_loc.lst_body_loc, "列表数据")
        if "UI自动化" not in lst_table_text:
            print("未搜索到数据")
            raise
        el_uiep = self.driver.find_elements(*qbm_loc.lst_body_row_loc)
        uiep_count = len(el_uiep)
        print("列表数据搜索条数【", uiep_count, "条】")
        self.click_element(ep_loc.ep_lst_reset_loc, "试卷管理-列表搜索-点击重置按钮")
        time.sleep(5)
        el_reset = self.driver.find_elements(*qbm_loc.lst_body_row_loc)
        el_reset_count = len(el_reset)
        print("列表数据搜索条数【", el_reset_count, "条】")
        if el_reset_count <= el_uiep:
            print("重置未生效")
            raise
        self.input_text(ep_loc.ep_lst_name_input_loc, "试卷名称搜索框", ep_lst_name_data)
        self.click_element(ep_loc.ep_lst_search_loc, "试卷管理-列表搜索-点击搜索")
        time.sleep(5)
        self.click_element(qbm_loc.del_row_loc, "点击列表数据删除按钮")
        time.sleep(5)
        self.click_element(qbm_loc.alert_del_loc, "点击确认删除")
        time.sleep(15)




